package com.yixiang.np.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yixiang.np.constant.GlobalConstant;
import com.yixiang.np.dao.ResourceDao;
import com.yixiang.np.dao.RoleDao;
import com.yixiang.np.model.PageFilter;
import com.yixiang.np.model.Tree;
import com.yixiang.np.model.po.Role;
import com.yixiang.np.service.RoleServiceI;



@Service
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public class RoleServiceImpl implements RoleServiceI {

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private ResourceDao resourceDao;

	@Override
	public List<Role> dataGrid(Role role, PageFilter ph) {
		List<Role> ul = new ArrayList<Role>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("whereSql", whereSql(role));
		params.put("orderSql", orderSql(ph));
		params.put("limitSql", limitSql(ph));
		ul = roleDao.findAll(params);
		return ul;
	}

	@Override
	public Long count(Role role, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("whereSql", whereSql(role));
		params.put("orderSql", orderSql(ph));
		params.put("limitSql", limitSql(ph));
		Long number = roleDao.count(params);
		return number;
	}
	
	private String whereSql(Role role) {
		String sql = "";
		if (role != null) {
			sql += " where 1=1 ";
			if (role.getName() != null) {
				sql += " and name like %" + role.getName() + "%";
			}
		}
		return sql;
	}
	private String orderSql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by " + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	private String limitSql(PageFilter ph) {
		int start = (ph.getPage()-1)*ph.getRows();
		String limitString  = " limit " + start + "," + ph.getRows();
		return limitString;
	}
	@Override
	public void add(Role role) {
		role.setIsdefault(GlobalConstant.NOT_DEFAULT);
		roleDao.add(role);
	}

	@Override
	public void delete(Long id) {
		roleDao.deleteRoleUser(id);
		roleDao.deleteRoleResource(id);
		roleDao.delete(id);
	}

	@Override
	public void edit(Role role) {
		roleDao.update(role);
	}

	@Override
	public Role get(Long id) {
		List<String> list = roleDao.getResourceIds(id);
		String ids = "";
		if(list!=null){
			boolean b = false;
			for (String tr : list) {
				if (b) {
					ids += ",";
				} else {
					b = true;
				}
				ids += tr;
			}
		}
		Role r = roleDao.get(id);
		if(r!=null){
			r.setResourceIds(ids);
		}
		return r;
	}

	@Override
	public void grant(Role role) {
		roleDao.deleteRoleResource(role.getId());
		if ((role.getResourceIds() != null) && !role.getResourceIds().equalsIgnoreCase("")) {
			Map<String, Object> params = new HashMap<String, Object>();
			for (String id : role.getResourceIds().split(",")) {
				params.put("role_id", role.getId());
				params.put("resource_id", id);
				roleDao.saveGrant(params);
			}
		} else {
			
		}
	}

	@Override
	public List<Tree> tree() {
		List<Role> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		l = roleDao.findAll(null);

		if ((l != null) && (l.size() > 0)) {
			for (Role r : l) {
				Tree tree = new Tree();
				tree.setId(r.getId().toString());
				tree.setText(r.getName());
				lt.add(tree);
			}
		}
		return lt;
	}
 
}
